﻿namespace AuthenticationUsingForms.Controllers
{
	using System;
	using System.Web.Security;

	using Castle.MonoRail.Framework;

	[Layout("default")]
	public class LoginController : SmartDispatcherController
	{
		public void Index()
		{
		}
		
		public void LogIn(String username, String password, bool rememberme, string ReturnUrl)
		{		
			if (FormsAuthentication.Authenticate(username, password))
			{
				CancelView();
				
				FormsAuthentication.RedirectFromLoginPage(username, rememberme, Context.ApplicationPath);

				FormsAuthentication.SetAuthCookie(username, rememberme, Context.ApplicationPath);

				if (ReturnUrl != null)
					Redirect(ReturnUrl);
				else
					Redirect("home", "index");
				
				return;
			}
			
			// If we got here then something is wrong with the supplied username/password
			
			Flash["error"] = "Invalid user name or password. Try again.";
			RedirectToAction("Index", "ReturnUrl=" + ReturnUrl);
		}
	}
}
